package com.cskaoyan.controller.admin.authcontroller;

import com.cskaoyan.Utils.LogUtils;
import com.cskaoyan.Utils.MyUtils;
import com.cskaoyan.service.authservice.AuthService;
import com.cskaoyan.vo.BaseRespVo;
import com.cskaoyan.vo.LoginVo;
import net.sf.jsqlparser.expression.DateTimeLiteralExpression;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.Date;
import java.util.Map;

/**
 * @author hsp
 * @date 2021/5/8 21:49
 */
@Controller
public class AuthController {

    @Autowired
    AuthService authService;
    @Autowired
    LogUtils logUtils;
    @RequestMapping("admin/auth/login")
    @ResponseBody
    public BaseRespVo login(@RequestBody Map map,HttpServletRequest request) {//也可以使用JavaBean


        String token;
        try {

             token =  authService.login(map);
        } catch (AuthenticationException e) {
//          在ExceptionControllerAdvise.java的
//          @ExceptionHandler(AuthenticationException.class)中处理
//            return "http://localhost:8080/#/login";
            String username = (String) map.get("username");
            logUtils.insertLog(username,request.getRemoteAddr(),new Date(),1,"登录",false,"失败,用户账号或密码不正确");
            return BaseRespVo.fail("用户账号或密码不正确");
        }
        String username = (String) map.get("username");
        logUtils.insertLog(username,request.getRemoteAddr(),new Date(),1,"登录",true,null);

        return BaseRespVo.ok(token);
    }


    /**
     * @param map /admin/auth/info?token=c46f889a-77cb-4f11-b72e-d8b6d10635f3
     * @return {"roles":["超级管理员"],
     * "name":"admin123","
     * perms":["*"],
     * "avatar":"https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif"}
     * WxLoginVo loginVO = new WxLoginVo();
     *         loginVO.setName("admin123");
     *         loginVO.setAvatar("https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
     *         ArrayList<String> roles = new ArrayList<>();
     *         roles.add("超级管理员");
     *         loginVO.setRoles(roles);
     *         ArrayList<String> perms = new ArrayList<>();
     *         perms.add("*");
     *         loginVO.setPerms(perms);
     *         return BaseRespVo.ok(loginVO);
     */
    @RequestMapping("admin/auth/info")
    @ResponseBody
    public BaseRespVo info(String token) {
        LoginVo loginVo = authService.info(token);
        return BaseRespVo.ok(loginVo);
    }


    @RequestMapping("admin/auth/logout")
    @ResponseBody
    public BaseRespVo logout(HttpServletRequest request){
        String adminname = MyUtils.getAdminname();
        logUtils.insertLog(adminname,request.getRemoteAddr(),new Date(),1,"退出",true,null);
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
//        return "redirect:http://localhost:8080/#/login";
        return BaseRespVo.ok();
    }
}
